package com.ruoyi.station.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.station.domain.SEquipment;
import com.ruoyi.station.domain.SMachineCabinet;
import com.ruoyi.station.domain.SMachineRoom;
import com.ruoyi.station.domain.SStation;
import com.ruoyi.station.mapper.SMachineCabinetMapper;
import com.ruoyi.station.mapper.SMachineRoomMapper;
import com.ruoyi.station.mapper.SStationMapper;
import com.ruoyi.station.service.SMachineCabinetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 机柜Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-06
 */
@Slf4j
@Service
public class SMachineCabinetServiceImpl extends ServiceImpl<SMachineCabinetMapper, SMachineCabinet> implements SMachineCabinetService {
    @Autowired
    private SMachineCabinetMapper sMachineCabinetMapper;
    @Autowired
    private SMachineRoomMapper sMachineRoomMapper;
    @Autowired
    private SStationMapper sStationMapper;
    /**
     * 导入机柜数据
     * @param file
     * @return
     */
    @Override
    @Transactional
    public String importsSMachineCabinet(MultipartFile file) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<List<Object>> lists = FileUtils.parseUploadFile(file);
        int i = 3;
        for (List<Object> readCabinet : lists) {
            //所属站点
            String sStationName = String.valueOf(readCabinet.get(1)).trim();
            SStation sStation = sStationMapper.selectOne(new LambdaQueryWrapper<SStation>().eq(SStation::getStationName, sStationName).eq(SStation::getCreateBy,user.getUserName()));
            if (sStation == null ) return "第" + i +"行站点不存在！";
            //所属机房
            String machineRoomName = String.valueOf(readCabinet.get(2)).trim();
            SMachineRoom machineRoom = sMachineRoomMapper.selectOne(new LambdaQueryWrapper<SMachineRoom>().eq(SMachineRoom::getMachineRoomName, machineRoomName).eq(SMachineRoom::getCreateBy,user.getUserName()));
            if (machineRoom == null ) return "第" + i +"行机房不存在！";
            //机柜编号
            String machineCabinetNum = String.valueOf(readCabinet.get(3)).trim();
            if(StringUtils.isBlank(machineCabinetNum)) return "第" + i +"行机柜编号不正确！";
            //机柜尺寸
            String machineCabinetSize = String.valueOf(readCabinet.get(4)).trim();
            if(StringUtils.isBlank(machineCabinetSize)) return "第" + i +"行机柜尺寸不正确！";
            //电源
            String power = DictUtils.getDictValue("power_source", String.valueOf(readCabinet.get(5)).trim());
            if(StringUtils.isBlank(power)) return "第" + i +"行电源不存在！";

            SMachineCabinet build = SMachineCabinet.builder()
                    .station(sStation.getId())
                    .machineRoom(machineRoom.getId())
                    .machineCabinetNum(machineCabinetNum)
                    .machineCabinetSize(machineCabinetSize)
                    .power(power)
                    .fillBy(user.getUserName())
                    .fillById(String.valueOf(user.getUserId()))
                    .fillDept(user.getDept().getDeptName())
                    .fillDeptId(user.getDeptId())
                    .createBy(user.getUserName())
                    .createTime(DateUtil.date())
                    .build();
            int insert = sMachineCabinetMapper.insert(build);
            if (insert < 1) return "第" + i +"行保存出错了！";
            i++;
        }
        return "成功导入"+lists.size()+"条";
    }
}
